import random
import json
import base64

def get_proxy_list():
    fo = open("proxy_list.txt", "r")
    data = fo.read()
    fo.close()
    proxy_info = json.loads(data)
    return proxy_info

class ProxyMiddleware(object):
    proxy_info = get_proxy_list()

    def process_request(self, request, spider):
        proxy = random.choice(self.proxy_info['arr'])
        if proxy['user_pass'] is not None:
            request.meta['proxy'] = "http://%s" % proxy['ip_port']
            encoded_user_pass = base64.encodestring(proxy['user_pass'])
            request.headers['Proxy-Authorization'] = 'Basic ' + \
                encoded_user_pass
            print "**************ProxyMiddleware have pass************" + proxy['ip_port']
        else:
            print "**************ProxyMiddleware no pass************" + proxy['ip_port']
            request.meta['proxy'] = "http://%s" % proxy['ip_port']
